package com.qys.livesMall.user.controller.sys;

import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.user.domain.dto.user.*;
import com.qys.livesMall.user.domain.vo.role.SysRoleVO;
import com.qys.livesMall.user.domain.vo.user.SysUserVO;
import com.qys.livesMall.user.enums.UserMessageEnum;
import com.qys.livesMall.user.service.ISysRoleService;
import com.qys.livesMall.user.service.ISysUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 *
 * @author wanglei
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService iSysRoleService;

    @Autowired
    private MessageServer messageServer;


    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public R<QysPage<SysUserVO>> list(SysUserSearch search) {
        return R.ok(userService.selectUserList(search));
    }



    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = {"/", "/{userId}"})
    public R<SysUserVO> getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        if(userId==null){
            userId=SecurityUtils.getUserId();
        }
        if (userId!=null) {
            SysUserVO vo=userService.selectUserById(userId);
            if(vo!=null) {
                vo.setRoles(iSysRoleService.selectRolesByUserId(userId));
            }
            if (vo.getRoles()!=null){
                vo.setRoleIds(vo.getRoles().stream().map(SysRoleVO::getRoleId).collect(Collectors.toList()));
            }
           return R.ok(vo);

        }
        return R.fail("未发现用户");
    }

    /**
     * 新增用户
     */
    @PostMapping
    public R add(@Validated @RequestBody AddSysUser user) {
        if (userService.checkUserNameExist(user.getUserName())) {
            return messageServer.failMsg(UserMessageEnum.USER_IS_EXIST,user.getUserName());
            //return R.fail("新增用户'" + user.getUserName() + "'失败，账号已存在");
        }
        return R.ok(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @PutMapping
    public R edit(@Validated @RequestBody EditSysUser user) {

        return R.ok(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userIds}")
    public R remove(@PathVariable Long[] userIds) {
        if (ArrayUtils.contains(userIds,SecurityUtils.getUserId())) {
            return R.ok("当前用户不能删除");
        }
        return R.ok(userService.deleteUserByIds(userIds));
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public R resetPwd(@RequestBody EditUserPsd user) {

        return R.ok(userService.resetPwd(user));
    }

    /**
     * 状态修改
     */
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody EditUserStatus userStatus) {
        return userService.updateUserStatus(userStatus);
    }

    /**
     * 根据用户编号获取授权角色
     */
    @GetMapping("/authRole/{userId}")
    public R<List<SysRoleVO>> authRole(@PathVariable("userId") Long userId) {
        List<SysRoleVO> roles = iSysRoleService.selectRolesByUserId(userId);
        return R.ok(roles);
    }

    /**
     * 根据用户编号获取授权角色
     */
    @GetMapping("/iAuthRole")
    public R<List<SysRoleVO>> iAuthRole() {
        List<SysRoleVO> roles = iSysRoleService.selectRolesByUserId(SecurityUtils.getUserId());
        return R.ok(roles);
    }


    /**
     * 用户授权角色
     */
    @PutMapping("/authRole")
    public R insertAuthRole(@RequestBody EditUserRole editUserRole) {
        return userService.insertUserAuth(editUserRole.getUserId(), editUserRole.getRoles());
    }

}
